package com.book.system.servlet;

import com.book.system.common.R;
import com.book.system.entity.User;
import com.book.system.service.LendListService;
import com.book.system.service.impl.LendListServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Slf4j
@WebServlet(name = "LendServlet", urlPatterns = "/lendList")
public class LendListServlet extends BaseServlet {

    private static final LendListService lendListService = new LendListServiceImpl();


    /**
     * 获取所有借还数据
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    public R<List<Map<String, Object>>> getList(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String flag = request.getParameter("flag");
        // 调用业务逻辑层方法
        List<Map<String, Object>> list = lendListService.getList(Integer.valueOf(flag));
        return R.success(list);

    }

    /**
     * 催还方法
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String> updateById(HttpServletRequest request, HttpServletResponse response) {
        // 获取前端传递的参数
        String serNum = request.getParameter("ser_num");
        int i = lendListService.updateById(serNum);
        if (i == 1) {
            return R.success("催还成功");
        } else {
            return R.error("催还失败");
        }
    }

    /**
     * 根据借还编号删除数据
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String> deleteById(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String serNum = request.getParameter("ser_num");
        log.info("ser_num:{}", serNum);
        if (lendListService.deleteById(serNum) == 1) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 借阅图书
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.commom.R<java.lang.String>
     */
    public R<String> borrowingBook(HttpServletRequest request, HttpServletResponse response) {

        //获取前端参数
        String bookId = request.getParameter("book_id");
        //获取当前登陆者的id
        User user = (User) request.getSession().getAttribute("user");
        log.info("当前登陆者id为：{}", user);

        //调用业务逻辑层方法
        int i = lendListService.borrowingBook(user.getId(), Integer.valueOf(bookId));
        if (i == 1) {
            return R.success("借书成功");
        } else {
            return R.error("借书失败");
        }
    }

    /**
     * 查询当前登陆用户的借阅信息
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */
    public R<List<Map<String, Object>>> getReaderBookList(HttpServletRequest request, HttpServletResponse response) {

        User user = (User) request.getSession().getAttribute("user");
        log.info("当前登陆者id为：{}", user.getId());
        return R.success(lendListService.getReaderBookList(user.getId()));
    }
    /**
     * 归还图书
     * @param request  请求对象
     * @param response  响应对象
     * @return com.book.system.common.R<java.lang.String>
     */
    public R<String> returnBook(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String serNum = request.getParameter("ser_num");
        if (lendListService.returnBook(Integer.valueOf(serNum)) == 1) {
            return R.success("还书成功");
        } else {
            return R.error("还书失败");
        }
    }
    /**
     * 续借图书
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.lang.String>
     */
    public R<String> renewalOfLoan(HttpServletRequest request, HttpServletResponse response) {

        // 获取前端传递的参数
        String serNum = request.getParameter("ser_num");
        if (lendListService.renewalOfLoan(Integer.valueOf(serNum)) == 1) {
            return R.success("续借成功");
        } else {
            return R.error("续借失败");
        }
    }

    /**
     * 查询已经归还的图书
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.util.List < java.util.Map < java.lang.String, java.lang.Object>>>
     */

    public R<List<Map<String, Object>>> getReaderList(HttpServletRequest request, HttpServletResponse response) {

        User user = (User) request.getSession().getAttribute("user");
        log.info("当前登陆者id为：{}", user.getId());
        return R.success(lendListService.getReaderList(user.getId()));
    }

    /**
     * 查询当前用户的未还图书信息
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return list
     */
    public R<List<Map<String, Object>>> getUnreturnedBooksList(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
        log.info("当前登陆者id为：{}", user.getId());
        return R.success(lendListService.getUnreturnedBooksList(user.getId()));

    }

    /**
     * 挂失图书
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.lang.String>
     */
    public R<String> reportingLoss(HttpServletRequest request, HttpServletResponse response) {
        //获取前端参数
        String serNum = request.getParameter("ser_num");
        if (lendListService.reportingLoss(Integer.valueOf(serNum)) == 1) {
            return R.success("挂失成功");
        } else {
            return R.error("挂失失败");
        }
    }
    /**
     * 获取用户挂失的图书数据
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>
     */
    public R<List<Map<String, Object>>> getReportingLossList(HttpServletRequest request, HttpServletResponse response) {

        User user = (User) request.getSession().getAttribute("user");
        log.info("当前登陆者id为：{}", user.getId());
        return R.success(lendListService.getReportingLossList(user.getId()));
    }

    /**
     * 解挂图书
     *
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.lang.String>
     */

    public R<String> unhook(HttpServletRequest request, HttpServletResponse response) {

        //获取前端参数
        String serNum = request.getParameter("ser_num");
        if (lendListService.unhook(Integer.valueOf(serNum)) == 1) {
            return R.success("挂失成功");
        } else {
            return R.error("挂失失败");
        }
    }

    /**
     * 赔偿图书方法
     * @param request  请求对象
     * @param response 响应对象
     * @return com.book.system.common.R<java.lang.String>
     */

    public R<String> compensateFor(HttpServletRequest request, HttpServletResponse response) {

        return null;
    }

}
